package com.hlj.smartpad.launcher

import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.text.TextUtils


class AppUtils {
    companion object {
        fun isApkInstalled(context: Context, packageName: Array<String>): Boolean {
            packageName.forEach { item ->
                if (TextUtils.isEmpty(item)) {
                    return false
                }

                try {
                    val info: ApplicationInfo = context.getPackageManager()
                        .getApplicationInfo(item, PackageManager.GET_UNINSTALLED_PACKAGES)
                    return true
                } catch (e: PackageManager.NameNotFoundException) {
                    e.printStackTrace()
                    if (packageName.indexOf(item) == packageName.size - 1) {
                        return false
                    }
                }
            }
            return false
        }

        fun isTopActivity(context: Context, packageName: Array<String>): Boolean {
            val activityManager =
                context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            val tasksInfo = activityManager.getRunningTasks(1)
            if (tasksInfo.size > 0) {
                packageName.forEach { item ->
                    if (item == tasksInfo[0].topActivity!!.packageName) {
                        return true
                    }
                }
            }
            return false
        }

        fun isAppRunning(context: Context, packageName: Array<String>): Boolean {
            val am: ActivityManager =
                context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            val list = am.getRunningTasks(100)
            if (list.isEmpty()) {
                return false
            }
            for (info: ActivityManager.RunningTaskInfo in list) {
                packageName.forEach { item ->
                    if (info.baseActivity?.packageName.equals(item)) {
                        return true
                    }
                }
            }
            return false
        }

        fun reStartApp(context: Context, packageName: Array<String>) {
            if (isApkInstalled(context, packageName)) {
                if (!isAppRunning(context, packageName)) {
                    packageName.forEach { item ->
                        val intent = context.packageManager.getLaunchIntentForPackage(item)
                        if (intent != null) {
                            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                            context.startActivity(intent)
                            return
                        }
                    }
                }
            }
        }

        fun getAppInfo(
            context: Context,
            packageName: ArrayList<String>
        ): MutableList<MutableMap<String, Any>> {
            val appMap: MutableMap<String, Any> = mutableMapOf()
            val appArray: MutableList<MutableMap<String, Any>> = mutableListOf()

            val intent = Intent(Intent.ACTION_MAIN, null).apply {
                addCategory(Intent.CATEGORY_LAUNCHER)
            }
            context.packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL).forEach { it: ResolveInfo ->
                packageName.forEach { name ->
                    if (it.activityInfo.packageName == name) {
                        val drawable =
                            it.activityInfo.loadIcon(context.packageManager) as BitmapDrawable
                        val bitmap: Bitmap = drawable.bitmap
                        val path = BitmapUtils.saveBitmap(
                            "" + System.currentTimeMillis(),
                            bitmap,
                            context
                        );
                        appMap["icon"] = path
                        appMap["label"] = it.loadLabel(context.packageManager)
                        appMap["packageName"] = it.activityInfo.packageName
                        appMap["appName"] = it.activityInfo.name
                        appArray.add(appMap)
                    }
                }
            }
            return appArray
        }
    }
}